﻿package 
{
    import com.assist.*;
    import com.assist.view.controls.*;
    import com.assist.view.interfaces.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    import flash.utils.*;

    public class oAlert extends Sprite implements IAlert
    {
        public var _Content:TextField;
        public var _Checked:MovieClip;
        public var _Yes:MovieClip;
        public var _No:MovieClip;
        public var _Cancel:MovieClip;
        private var _yesLabelTip:String = "";
        private var _noLabelTip:String = "";
        private var _cancelLabelTip:String = "";
        private var _yesLabel:String = "Yes";
        private var _noLabel:String = "No";
        private var _cancelLabel:String = "Cancel";
        private var _yesEnabled:Boolean = true;
        private var _noEnabled:Boolean = true;
        private var _cancelEnabled:Boolean = true;
        private var _firstButton:uint = 1;
        private var _hasCheckbox:Boolean = false;
        private var _tip:ITip;
        private var _parent:Sprite;
        private var _visible:Boolean = false;
        private var _closeHander:Function;
        private var _defaultPosition:Dictionary;
        private var _infoDefaultWidth:int = 238;
        private var _defaultX:Number;
        private var _defaultY:Number;
        private var _defaultW:Number;
        private var _defaultH:Number;
        private var _minW:int;
        private var _minH:int;
        private var _maxW:int;
        private var _maxH:int;
        private var _timerIn:Timer;
        private var _listIn:Array;
        private var _timerOut:Timer;
        private var _list:Array;

        public function oAlert()
        {
            this._defaultPosition = new Dictionary();
            this._listIn = [];
            this._list = [];
            this._Checked.buttonMode = true;
            this._Checked.useHandCursor = true;
            this.settleButton(this._Yes, this._yesLabel);
            this.settleButton(this._No, this._noLabel);
            this.settleButton(this._Cancel, this._cancelLabel);
            this._defaultW = width;
            this._defaultH = height;
            return;
        }// end function

        public function reposition(param1:int, param2:int, param3:int, param4:int) : void
        {
            if (param1)
            {
                this._minW = param1;
            }
            else
            {
                param1 = this._minW;
            }
            if (param2)
            {
                this._minH = param2;
            }
            else
            {
                param2 = this._minH;
            }
            if (param3)
            {
                this._maxW = param3;
            }
            else
            {
                param3 = this._maxW;
            }
            if (param4)
            {
                this._maxH = param4;
            }
            else
            {
                param4 = this._maxH;
            }
            if (this._parent == null || parent == null)
            {
                return;
            }
            var _loc_5:* = Math.max(param1, Math.min(param3, stage.stageWidth));
            var _loc_6:* = Math.max(param2, Math.min(param4, stage.stageHeight));
            this._parent.graphics.clear();
            this._parent.graphics.beginFill(0, 0);
            this._parent.graphics.drawRect(0, 0, this._parent.stage.stageWidth, this._parent.stage.stageHeight);
            this._parent.graphics.endFill();
            x = Math.floor((_loc_5 - width) / 2);
            y = Math.floor((_loc_6 - height) / 2);
            this._defaultX = x;
            this._defaultY = y;
            return;
        }// end function

        public function show(param1:String, param2:uint = 1, param3:Function = null) : void
        {
            this.formatText(param1);
            this._closeHander = param3;
            this.buttonState(this._Yes, this._yesLabel, this._yesLabelTip, this._yesEnabled);
            this.buttonState(this._No, this._noLabel, this._noLabelTip, this._noEnabled);
            this.buttonState(this._Cancel, this._cancelLabel, this._cancelLabelTip, this._cancelEnabled);
            var _loc_4:Array = [];
            switch(param2)
            {
                case 1:
                {
                    this.yesVisible = true;
                    _loc_4.push(this._Yes);
                    this._firstButton = AlertButtonType.Yes;
                    break;
                }
                case 2:
                {
                    this.noVisible = true;
                    _loc_4.push(this._No);
                    this._firstButton = AlertButtonType.No;
                    break;
                }
                case 3:
                {
                    this.yesVisible = true;
                    this.noVisible = true;
                    _loc_4.push(this._Yes, this._No);
                    this._firstButton = AlertButtonType.Yes;
                    break;
                }
                case 4:
                {
                    this.cancelVisible = true;
                    _loc_4.push(this._Cancel);
                    this._firstButton = AlertButtonType.Cancel;
                    break;
                }
                case 5:
                {
                    this.yesVisible = true;
                    this.cancelVisible = true;
                    _loc_4.push(this._Yes, this._Cancel);
                    this._firstButton = AlertButtonType.Yes;
                    break;
                }
                case 6:
                {
                    this.noVisible = true;
                    this.cancelVisible = true;
                    _loc_4.push(this._No, this._Cancel);
                    this._firstButton = AlertButtonType.No;
                    break;
                }
                case 7:
                {
                    this.yesVisible = true;
                    this.noVisible = true;
                    this.cancelVisible = true;
                    _loc_4.push(this._Yes, this._No, this._Cancel);
                    this._firstButton = AlertButtonType.Yes;
                    break;
                }
                default:
                {
                    this.yesVisible = true;
                    _loc_4.push(this._Yes);
                    this._firstButton = AlertButtonType.Yes;
                    break;
                }
            }
            var _loc_5:int = 10;
            var _loc_6:* = (_loc_4.length - 1) * _loc_5 + _loc_4[0].width * _loc_4.length;
            var _loc_7:* = (width - _loc_6) / 2;
            var _loc_8:int = 0;
            while (_loc_8 < _loc_4.length)
            {
                
                _loc_4[_loc_8].x = _loc_7 + _loc_8 * (_loc_4[_loc_8].width + _loc_5);
                _loc_8++;
            }
            this._Checked.visible = this._hasCheckbox;
            this._Checked._Box.gotoAndStop(1);
            this._Checked.addEventListener(MouseEvent.CLICK, this.onCheckedHandler);
            if (this._parent)
            {
                this._parent.addChild(this);
                this._visible = true;
            }
            this.reposition(0, 0, 0, 0);
            visible = false;
            this.startFadeIn();
            return;
        }// end function

        public function updateContent(param1:String) : void
        {
            this.formatText(param1);
            return;
        }// end function

        private function formatText(param1:String) : void
        {
            this._Content.htmlText = param1;
            this._Content.autoSize = TextFieldAutoSize.LEFT;
            this._Content.multiline = false;
            this._Content.wordWrap = false;
            var _loc_2:* = new TextFormat();
            _loc_2.leading = 5;
            _loc_2.align = "center";
            this._Content.setTextFormat(_loc_2);
            var _loc_3:* = /\r|\
n""\r|\n/.test(param1);
            if (this._Content.width > this._infoDefaultWidth || _loc_3)
            {
                _loc_2.align = _loc_3 ? ("center") : ("left");
                this._Content.y = 29;
                this._Content.height = 50;
                this._Content.multiline = true;
                this._Content.wordWrap = true;
            }
            else
            {
                this._Content.y = 38;
                this._Content.height = 18;
            }
            this._Content.autoSize = "none";
            this._Content.width = this._infoDefaultWidth;
            this._Content.setTextFormat(_loc_2);
            return;
        }// end function

        public function hide() : void
        {
            this.startFadeOut();
            if (this._parent)
            {
                this._parent.graphics.clear();
            }
            if (parent)
            {
                parent.removeChild(this);
            }
            if (this._tip)
            {
                this._tip.hide();
                this._tip.removeTarget(this._Yes);
                this._tip.removeTarget(this._No);
                this._tip.removeTarget(this._Cancel);
            }
            this._yesLabelTip = "";
            this._noLabelTip = "";
            this._cancelLabelTip = "";
            this._yesLabel = "Yes";
            this._noLabel = "No";
            this._cancelLabel = "Cancel";
            this._yesEnabled = true;
            this._noEnabled = true;
            this._cancelEnabled = true;
            this._hasCheckbox = false;
            this._visible = false;
            return;
        }// end function

        private function onCheckedHandler(event:MouseEvent) : void
        {
            var _loc_2:* = this._Checked._Box.currentFrame;
            this._Checked._Box.gotoAndStop(_loc_2 == 1 ? (2) : (1));
            return;
        }// end function

        private function settleButton(param1:MovieClip, param2:String) : void
        {
            param1._Name.text = param2;
            param1._Name.mouseEnabled = false;
            param1.buttonMode = true;
            param1.visible = true;
            this._defaultPosition[param1] = new Point(param1._Name.x, param1._Name.y);
            param1.addEventListener(MouseEvent.MOUSE_OVER, this.onMouseOverHandler);
            param1.addEventListener(MouseEvent.MOUSE_OUT, this.onMouseOutHandler);
            param1.addEventListener(MouseEvent.MOUSE_DOWN, this.onMouseDownHandler);
            param1.addEventListener(MouseEvent.MOUSE_UP, this.onMouseUpHandler);
            param1.addEventListener(MouseEvent.CLICK, this.onClickHandler);
            return;
        }// end function

        private function onMouseOverHandler(event:MouseEvent) : void
        {
            event.target.gotoAndStop(2);
            return;
        }// end function

        private function onMouseOutHandler(event:MouseEvent) : void
        {
            event.target.gotoAndStop(1);
            event.target._Name.x = this._defaultPosition[event.target].x;
            event.target._Name.y = this._defaultPosition[event.target].y;
            return;
        }// end function

        private function onMouseDownHandler(event:MouseEvent) : void
        {
            event.target.gotoAndStop(3);
            event.target._Name.x = this._defaultPosition[event.target].x + 1;
            event.target._Name.y = this._defaultPosition[event.target].y + 1;
            return;
        }// end function

        private function onMouseUpHandler(event:MouseEvent) : void
        {
            event.target.gotoAndStop(2);
            event.target._Name.x = this._defaultPosition[event.target].x;
            event.target._Name.y = this._defaultPosition[event.target].y;
            return;
        }// end function

        private function onClickHandler(event:MouseEvent) : void
        {
            var _loc_2:* = AlertButtonType.Yes;
            if (event.target == this._No)
            {
                _loc_2 = AlertButtonType.No;
            }
            else if (event.target == this._Cancel)
            {
                _loc_2 = AlertButtonType.Cancel;
            }
            this.hide();
            if (this._closeHander is Function)
            {
                this._closeHander(_loc_2);
            }
            return;
        }// end function

        private function set yesVisible(param1:Boolean) : void
        {
            this._Yes.visible = param1;
            return;
        }// end function

        private function set noVisible(param1:Boolean) : void
        {
            this._No.visible = param1;
            return;
        }// end function

        private function set cancelVisible(param1:Boolean) : void
        {
            this._Cancel.visible = param1;
            return;
        }// end function

        private function buttonState(param1:MovieClip, param2:String, param3:String, param4:Boolean) : void
        {
            param1.visible = false;
            param1._Name.text = param2;
            param1.mouseEnabled = param4;
            param1.filters = param4 ? ([]) : ([Filter.filter1]);
            if (param3)
            {
                this._tip.addTarget(param1, param3);
            }
            return;
        }// end function

        public function defaultCloseEvent() : void
        {
            this.hide();
            if (this._closeHander is Function)
            {
                this._closeHander(this._firstButton);
            }
            return;
        }// end function

        private function startFadeIn() : void
        {
            if (this._parent == null)
            {
                return;
            }
            if (this._timerIn == null)
            {
                this._timerIn = new Timer(10);
                this._timerIn.addEventListener(TimerEvent.TIMER, this.onFadeIn);
            }
            var _loc_1:* = new BitmapData(width, height, true, 0);
            _loc_1.draw(this);
            var _loc_2:* = new Bitmap(_loc_1);
            this._parent.addChild(_loc_2);
            _loc_2.scaleX = 0.5;
            _loc_2.scaleY = 0.5;
            _loc_2.x = this._defaultX + (this._defaultW - _loc_2.width) / 2;
            _loc_2.y = this._defaultY + (this._defaultH - _loc_2.height) / 2;
            this._listIn.push(_loc_2);
            if (this._listIn.length == 1)
            {
                this._timerIn.start();
            }
            return;
        }// end function

        private function onFadeIn(event:TimerEvent) : void
        {
            var _loc_4:Bitmap = null;
            var _loc_2:* = this._listIn.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 > -1)
            {
                
                _loc_4 = this._listIn[_loc_3];
                if (_loc_4.scaleX < 1)
                {
                    _loc_4.scaleX = _loc_4.scaleX + 0.1;
                    _loc_4.scaleY = _loc_4.scaleY + 0.1;
                    _loc_4.x = this._defaultX + (this._defaultW - _loc_4.width) / 2;
                    _loc_4.y = this._defaultY + (this._defaultH - _loc_4.height) / 2;
                }
                if (_loc_4.scaleX > 0.9)
                {
                    this._parent.removeChild(_loc_4);
                    this._listIn.splice(_loc_3, 1);
                    visible = true;
                }
                _loc_3 = _loc_3 - 1;
            }
            if (this._listIn.length == 0)
            {
                this._timerIn.stop();
            }
            return;
        }// end function

        private function startFadeOut() : void
        {
            if (this._parent == null || parent == null)
            {
                return;
            }
            if (this._timerOut == null)
            {
                this._timerOut = new Timer(10);
                this._timerOut.addEventListener(TimerEvent.TIMER, this.onFadeOut);
            }
            var _loc_1:* = new BitmapData(width, height, true, 0);
            _loc_1.draw(this);
            var _loc_2:* = new Bitmap(_loc_1);
            this._parent.addChild(_loc_2);
            _loc_2.x = x;
            _loc_2.y = y;
            this._list.push(_loc_2);
            if (this._list.length == 1)
            {
                this._timerOut.start();
            }
            return;
        }// end function

        private function onFadeOut(event:TimerEvent) : void
        {
            var _loc_4:Bitmap = null;
            var _loc_2:* = this._list.length;
            var _loc_3:* = _loc_2 - 1;
            while (_loc_3 > -1)
            {
                
                _loc_4 = this._list[_loc_3];
                this._list[_loc_3].alpha = _loc_4.alpha - 0.15;
                if (_loc_4.scaleX - 0.08 > 0)
                {
                    _loc_4.scaleX = _loc_4.scaleX - 0.08;
                    _loc_4.scaleY = _loc_4.scaleY - 0.08;
                    _loc_4.x = this._defaultX + (this._defaultW - _loc_4.width) / 2;
                    _loc_4.y = this._defaultY + (this._defaultH - _loc_4.height) / 2;
                }
                if (_loc_4.alpha <= 0)
                {
                    _loc_4.parent.removeChild(_loc_4);
                    this._list.splice(_loc_3, 1);
                }
                _loc_3 = _loc_3 - 1;
            }
            if (this._list.length == 0)
            {
                this._timerOut.stop();
            }
            return;
        }// end function

        public function get content() : Sprite
        {
            return this;
        }// end function

        public function set oParent(param1:Sprite) : void
        {
            this._parent = param1;
            return;
        }// end function

        public function set tip(param1:ITip) : void
        {
            this._tip = param1;
            return;
        }// end function

        public function set yesLabelTip(param1:String) : void
        {
            this._yesLabelTip = param1;
            return;
        }// end function

        public function set noLabelTip(param1:String) : void
        {
            this._noLabelTip = param1;
            return;
        }// end function

        public function set cancelLabelTip(param1:String) : void
        {
            this._cancelLabelTip = param1;
            return;
        }// end function

        public function set yesLabel(param1:String) : void
        {
            this._yesLabel = param1;
            return;
        }// end function

        public function set noLabel(param1:String) : void
        {
            this._noLabel = param1;
            return;
        }// end function

        public function set cancelLabel(param1:String) : void
        {
            this._cancelLabel = param1;
            return;
        }// end function

        public function set yesEnabled(param1:Boolean) : void
        {
            this._yesEnabled = param1;
            return;
        }// end function

        public function set noEnabled(param1:Boolean) : void
        {
            this._noEnabled = param1;
            return;
        }// end function

        public function set cancelEnabled(param1:Boolean) : void
        {
            this._cancelEnabled = param1;
            return;
        }// end function

        public function set hasCheckbox(param1:Boolean) : void
        {
            this._hasCheckbox = param1;
            return;
        }// end function

        public function get checked() : Boolean
        {
            return this._Checked._Box.currentFrame == 2;
        }// end function

        override public function get visible() : Boolean
        {
            return this._visible;
        }// end function

    }
}
